package com.songshuai.sales.client.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.songshuai.sales.client.service.WxPlatformService;
import com.songshuai.webapi.baseapi.enumerations.miniprogram.ResponseStatus;
import com.songshuai.webapi.baseapi.exception.BusinessException;
import com.songshuai.webapi.baseapi.model.sales.WxPlatform;
import com.songshuai.webapi.common.httpclient.HttpClientUtil;
import com.songshuai.webapi.common.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class AccessToken {

    @Autowired
    private WxPlatformService wxPlatformService;

    @Autowired
    private RedisUtil redisUtil;



    //@Lazy(false)
    //@Scheduled(cron = "0 */90 * * * ? ")   //每90分钟执行一次
    public void text1() {
        System.out.println("定时任务启动了");
        // 小程序唯一标识 (在微信小程序管理后台获取)
        String wxspAppid = "wxb2cb3caa4ff29b27";
        // 小程序的 app secret (在微信小程序管理后台获取)
        String wxspSecret = "f5c96e1d1241463746731b651829c045";
        //这里直接写死就可以，不用改，用法可以去看api
        String grant_type = "client_credential";
        //封装请求数据
        String params = "grant_type=" + grant_type + "&secret=" + wxspSecret + "&appid=" + wxspAppid;
        //发送GET请求
        String sendGet = HttpClientUtil.sendGet("https://api.weixin.qq.com/cgi-bin/token", params);
        // 解析相应内容（转换成json对象）
        JSONObject json = JSON.parseObject(sendGet);
        //拿到accesstoken
        if(ResponseStatus.ACCESS_TOKEN_ERRCODE.getErrorCode() == json.getInteger("errcode")){
            throw new BusinessException(ResponseStatus.ACCESS_TOKEN_ERRCODE);
        }
        if(ResponseStatus.ACCESS_TOKEN_ERRCODE_40001.getErrorCode() == json.getInteger("errcode")){
            throw new BusinessException(ResponseStatus.ACCESS_TOKEN_ERRCODE_40001);
        }
        if(ResponseStatus.ACCESS_TOKEN_ERRCODE_40002.getErrorCode() == json.getInteger("errcode")){
            throw new BusinessException(ResponseStatus.ACCESS_TOKEN_ERRCODE_40002);
        }
        if(ResponseStatus.ACCESS_TOKEN_ERRCODE_40013.getErrorCode() == json.getInteger("errcode")){
            throw new BusinessException(ResponseStatus.ACCESS_TOKEN_ERRCODE_40013);
        }
        /*//更新数据库access_token
        WxPlatform wxPlatform = new WxPlatform();
        wxPlatform.setAppId(wxspAppid);
        WxPlatform result = wxPlatformService.selectByEntity(wxPlatform);
        wxPlatform.setAppSecret(wxspSecret);
        wxPlatform.setAccessToken(json.getString("access_token"));
        wxPlatform.setExpiresIn(json.getInteger("expires_in"));
        wxPlatform.setUpdateTime(new Date());
        if(result != null){
            //数据库存在，则更新
            wxPlatform.setId(result.getId());
            wxPlatformService.updateByPrimaryKey(wxPlatform);
        } else {
            //数据库不存在
            wxPlatformService.insertSelective(wxPlatform);
        }*/
        //更新缓存access_token
        redisUtil.set(wxspAppid,json.getString("access_token"),7200);
    }
}